//
//  MYGAcademicConferenceInteractInputView.swift
//  MYDoctor
//
//  Created by Denny on 2024/5/28.
//

import UIKit

class MYGAcademicConferenceInteractInputView: UIView,UITextViewDelegate {
    
    var tapSendCompeltion:((String)->Void)?
    var tapCancelCompeltion:(()->Void)?
    
    private lazy var cancelBtn: UIButton = {
        let control = UIButton()
        control.setTitle("取消", for: .normal)
        control.setTitleColor(UIColor.init(hexString: "666666"), for: .normal)
        control.titleLabel?.font = .regular(15)
        control.addTarget(self, action: #selector(tapCancel), for: .touchUpInside)
       return control
    }()
    
    private lazy var sureBtn: UIButton = {
         let control = UIButton()
         control.setTitle("发布", for: .normal)
         control.setTitleColor(UIColor.init(hexString: "4583FA"), for: .normal)
         control.titleLabel?.font = .regular(15)
         control.addTarget(self, action: #selector(tapSubmit), for: .touchUpInside)
        return control
     }()

    var titleLB:UILabel = {
        let control = UILabel()
        control.font = .regular(16)
        control.textColor = UIColor.init(hexString: "232323")
        control.textAlignment = .center
        return control
    }()
      var contentTF:UITextView = {
        let control = UITextView()
        control.returnKeyType = .done
        return control
    }()
    
    private lazy var line:UIView = {
        let control = UIView()
        control.backgroundColor = UIColor.init(hexString: "eeeeee")
        return control
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        addSubviews([cancelBtn,titleLB,sureBtn,line,contentTF])
        cancelBtn.snp.makeConstraints { make in
            make.left.top.equalToSuperview()
            make.size.equalTo(CGSize.init(width: 50, height: 40))
        }
        titleLB.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.height.equalTo(40)
            make.centerX.equalToSuperview()
        }
        
        sureBtn.snp.makeConstraints { make in
            make.right.top.equalToSuperview()
            make.size.equalTo(CGSize.init(width: 50, height: 40))
        }
        line.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(60)
            make.left.right.equalToSuperview()
            make.height.equalTo(0.5)
        }
        contentTF.snp.makeConstraints { make in
            make.top.equalTo(line.snp.bottom).offset(8)
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.height.equalTo(125)
        }
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
            self.contentTF.placeholderText = "简单说两句吧，分享您的观看想法～"
        })
        
        contentTF.delegate = self

    }
    
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        if text == "\n" { //判断输入的字是否是回车，即按下return
            //在这里做你响应return键的代码
            tapSubmit()
            return false //这里返回NO，就代表return键值失效，即页面上按下return，不会出现换行，如果为yes，则输入页面会换行
        }
        return true
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    @objc func tapCancel() {
        contentTF.resignFirstResponder()
        if let c = tapCancelCompeltion {
            c()
        }
    }
    
    @objc func tapSubmit() {
        if let c = tapSendCompeltion,let text = contentTF.text {
            if text.count > 0 {
                contentTF.text = ""
                c(text)
                contentTF.resignFirstResponder()
            }
        }
    }
  
    
    
}
